/**
 *    Copyright 2010 Merrick Zhang ( anphorea@gmail.com )
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package anpho.proxyvalidator.proc;

import java.net.Proxy;

import anpho.proxyvalidator.bean.ProxyBean;
import anpho.proxyvalidator.config.AppContext;

/**
 * @author Merrick
 */
public class ValidatorThread implements Runnable {

	ValidatorMgr	m;

	int				RETRY_COUNT	= AppContext.getInt("RETRY_COUNT");

	/*
	 * (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		Proxy p = m.getNextProxy();
		// while the Input pool is not empty,get the next proxy.
		while (p != null) {
			// build ProxyBean
			ProxyBean pb = new ProxyBean(p, RETRY_COUNT);

			// validate for RETRY_COUNT times.
			for (int i = 0; i < RETRY_COUNT; i++) {
				pb.respTimes[i] = m.validateProxy(p);
			}

			if (1.00 - pb.getFailRate() < 1e-7) {

			} else {
				// CallBack method.put the result back to the host.
				m.addResult(pb);
			}
			p = m.getNextProxy();
		}
		m.NotifyExit(Thread.currentThread());

	}

	/**
	 * if RETRY_COUNT is not set,it will be set to 5 here.
	 */
	public ValidatorThread(ValidatorMgr mt) {
		m = mt;
		if (RETRY_COUNT == 0) {
			RETRY_COUNT = 5;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#finalize()
	 */
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
	}
}
